package com.zhaoqi.hr.controller;

import com.zhaoqi.hr.dto.request.LoginRequest;
import com.zhaoqi.hr.dto.request.RegisterRequest;
import com.zhaoqi.hr.dto.request.WechatPhoneLoginRequest;
import com.zhaoqi.hr.dto.response.ApiResponse;
import com.zhaoqi.hr.dto.response.JwtResponse;
import com.zhaoqi.hr.entity.User;
import com.zhaoqi.hr.service.AuthService;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    private static final Logger log = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    private AuthService authService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public ApiResponse<User> register(@Valid @RequestBody RegisterRequest request) {
        try {
            User user = authService.register(request);
            return ApiResponse.success("注册成功", user);
        } catch (Exception e) {
            log.error("用户注册失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ApiResponse<JwtResponse> login(@Valid @RequestBody LoginRequest request) {
        try {
            JwtResponse response = authService.login(request);
            return ApiResponse.success("登录成功", response);
        } catch (Exception e) {
            log.error("用户登录失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }

    /**
     * 发送短信验证码
     */
    @PostMapping("/sms/send")
    public ApiResponse<Void> sendSmsCode(
            @RequestParam @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") String phone) {
        try {
            authService.sendSmsCode(phone);
            return ApiResponse.success("验证码发送成功", null);
        } catch (Exception e) {
            log.error("发送验证码失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }

    /**
     * 微信手机号授权登录
     */
    @PostMapping("/wechat/phone-login")
    public ApiResponse<JwtResponse> wechatPhoneLogin(@Valid @RequestBody WechatPhoneLoginRequest request) {
        try {
            JwtResponse response = authService.wechatPhoneLogin(request);
            return ApiResponse.success("微信授权登录成功", response);
        } catch (Exception e) {
            log.error("微信授权登录失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }

    /**
     * 当前手机号登录
     */
    @PostMapping("/current-phone-login")
    public ApiResponse<JwtResponse> currentPhoneLogin(@Valid @RequestBody WechatPhoneLoginRequest request) {
        try {
            JwtResponse response = authService.currentPhoneLogin(request);
            return ApiResponse.success("当前手机号登录成功", response);
        } catch (Exception e) {
            log.error("当前手机号登录失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/me")
    public ApiResponse<User> getCurrentUser() {
        try {
            User user = authService.getCurrentUser();
            return ApiResponse.success(user);
        } catch (Exception e) {
            log.error("获取用户信息失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
}
